package com.laicunba.ext.web.controller.h5.activity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;

@Controller
@RequestMapping(value = "/h5/ext/activity")
public class Activity20170726gqkgController extends BaseController {

	@RequestMapping(value = "/20170726gqkg.htm")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView();
		view.setViewName("20170726gqkg.jsp");

		String couponCacheKey = "act.20170726gqkg.coupon." + DateUtil.formatDate(new Date());
		Map<String, String> couponMap = CacheUtil.hgetAll(couponCacheKey);
		if (couponMap == null || couponMap.isEmpty()) {
			couponMap = new HashMap<String, String>();
			couponMap.put("am.20170726.5k.1", "300");
			couponMap.put("am.20170726.5k.2", "300");
			couponMap.put("am.20170726.5k.3", "300");
			couponMap.put("am.20170726.5k.4", "300");
			couponMap.put("am.20170726.5k.5", "200");

			couponMap.put("pm.20170726.5k.1", "300");
			couponMap.put("pm.20170726.5k.2", "300");
			couponMap.put("pm.20170726.5k.3", "300");
			couponMap.put("pm.20170726.5k.4", "300");
			couponMap.put("pm.20170726.5k.5", "200");

			couponMap.put("am.20170726.1w.1", "300");
			couponMap.put("am.20170726.1w.2", "300");
			couponMap.put("am.20170726.1w.3", "300");
			couponMap.put("am.20170726.1w.4", "300");
			couponMap.put("am.20170726.1w.5", "200");

			couponMap.put("pm.20170726.1w.1", "300");
			couponMap.put("pm.20170726.1w.2", "300");
			couponMap.put("pm.20170726.1w.3", "300");
			couponMap.put("pm.20170726.1w.4", "300");
			couponMap.put("pm.20170726.1w.5", "200");

			couponMap.put("am.20170726.5w.1", "300");
			couponMap.put("am.20170726.5w.2", "300");
			couponMap.put("am.20170726.5w.3", "300");
			couponMap.put("am.20170726.5w.4", "300");
			couponMap.put("am.20170726.5w.5", "200");

			couponMap.put("pm.20170726.5w.1", "300");
			couponMap.put("pm.20170726.5w.2", "300");
			couponMap.put("pm.20170726.5w.3", "300");
			couponMap.put("pm.20170726.5w.4", "300");
			couponMap.put("pm.20170726.5w.5", "200");
			CacheUtil.hmset(couponCacheKey, couponMap);
			CacheUtil.expire(couponCacheKey, 3600 * 25);
		}
		
		view.addObject("is10Clock", new Date().getHours()>=10);
		view.addObject("coupon_map", couponMap);
		return view;
	}

	@ResponseBody
	@RequestMapping(value = "/20170726gqkg_store.htm")
	public String getStore() {
		String couponCacheKey = "act.20170726gqkg.coupon." + DateUtil.formatDate(new Date());
		Map<String, String> couponMap = CacheUtil.hgetAll(couponCacheKey);
		Map<String, Integer> couponStore = new HashMap();
		couponStore.put("store.5k.1", Integer.parseInt(couponMap.get("am.20170726.5k.1")));
		couponStore.put("store.5k.2", Integer.parseInt(couponMap.get("am.20170726.5k.2")));
		couponStore.put("store.5k.3", Integer.parseInt(couponMap.get("am.20170726.5k.3")));
		couponStore.put("store.5k.4", Integer.parseInt(couponMap.get("am.20170726.5k.4")));
		couponStore.put("store.5k.5", Integer.parseInt(couponMap.get("am.20170726.5k.5")));
		couponStore.put("store.1w.1", Integer.parseInt(couponMap.get("am.20170726.1w.1")));
		couponStore.put("store.1w.2", Integer.parseInt(couponMap.get("am.20170726.1w.2")));
		couponStore.put("store.1w.3", Integer.parseInt(couponMap.get("am.20170726.1w.3")));
		couponStore.put("store.1w.4", Integer.parseInt(couponMap.get("am.20170726.1w.4")));
		couponStore.put("store.1w.5", Integer.parseInt(couponMap.get("am.20170726.1w.5")));
		couponStore.put("store.5w.1", Integer.parseInt(couponMap.get("am.20170726.5w.1")));
		couponStore.put("store.5w.2", Integer.parseInt(couponMap.get("am.20170726.5w.2")));
		couponStore.put("store.5w.3", Integer.parseInt(couponMap.get("am.20170726.5w.3")));
		couponStore.put("store.5w.4", Integer.parseInt(couponMap.get("am.20170726.5w.4")));
		couponStore.put("store.5w.5", Integer.parseInt(couponMap.get("am.20170726.5w.5")));

		if (new Date().getHours() >= 15) {

			couponStore.put("store.5k.1", couponStore.get("store.5k.1") + Integer.parseInt(couponMap.get("pm.20170726.5k.1")));
			couponStore.put("store.5k.2", couponStore.get("store.5k.2") + Integer.parseInt(couponMap.get("pm.20170726.5k.2")));
			couponStore.put("store.5k.3", couponStore.get("store.5k.3") + Integer.parseInt(couponMap.get("pm.20170726.5k.3")));
			couponStore.put("store.5k.4", couponStore.get("store.5k.4") + Integer.parseInt(couponMap.get("pm.20170726.5k.4")));
			couponStore.put("store.5k.5", couponStore.get("store.5k.5") + Integer.parseInt(couponMap.get("pm.20170726.5k.5")));
			couponStore.put("store.1w.1", couponStore.get("store.1w.1") + Integer.parseInt(couponMap.get("pm.20170726.1w.1")));
			couponStore.put("store.1w.2", couponStore.get("store.1w.2") + Integer.parseInt(couponMap.get("pm.20170726.1w.2")));
			couponStore.put("store.1w.3", couponStore.get("store.1w.3") + Integer.parseInt(couponMap.get("pm.20170726.1w.3")));
			couponStore.put("store.1w.4", couponStore.get("store.1w.4") + Integer.parseInt(couponMap.get("pm.20170726.1w.4")));
			couponStore.put("store.1w.5", couponStore.get("store.1w.5") + Integer.parseInt(couponMap.get("pm.20170726.1w.5")));
			couponStore.put("store.5w.1", couponStore.get("store.5w.1") + Integer.parseInt(couponMap.get("pm.20170726.5w.1")));
			couponStore.put("store.5w.2", couponStore.get("store.5w.2") + Integer.parseInt(couponMap.get("pm.20170726.5w.2")));
			couponStore.put("store.5w.3", couponStore.get("store.5w.3") + Integer.parseInt(couponMap.get("pm.20170726.5w.3")));
			couponStore.put("store.5w.4", couponStore.get("store.5w.4") + Integer.parseInt(couponMap.get("pm.20170726.5w.4")));
			couponStore.put("store.5w.5", couponStore.get("store.5w.5") + Integer.parseInt(couponMap.get("pm.20170726.5w.5")));
		}

		return toJsonObject(couponStore);
	}
}
